package cn.kgc.javaweb.servlet;

import cn.hutool.captcha.LineCaptcha;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/5/16
 * @Description:  生成验证码
 */
@WebServlet("/captcha")
public class LineCaptchaServlet extends BaseServlet {

    protected void getCode(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        LineCaptcha lineCaptcha = new LineCaptcha(110, 50, 4, 3);
        //获取验证码
        String code = lineCaptcha.getCode();
        System.out.println("code = " + code);

        //保存验证码 用于登录认证
        HttpSession session = req.getSession();
        session.setAttribute("code",code);

        // 通过响应对象 获取输出流 将验证码图片写出到浏览器
        ServletOutputStream outputStream = resp.getOutputStream();
        lineCaptcha.write(outputStream);
        outputStream.close();
    }
}
